/*
 * @Descripttion: 全局loading
 * @version:
 * @Author: lhl
 * @Date: 2024-09-12 11:43:56
 * @LastEditors: lhl
 * @LastEditTime: 2024-09-12 11:45:33
 */

import { ElLoading } from 'element-plus';

let loadingInstance: ReturnType<typeof ElLoading.service>;

/**
 * 开启 Loading
 */
const startLoading = () => {
  loadingInstance = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0, 0, 0, 0.7)'
  });
};

/**
 * 关闭 Loading
 */
const endLoading = () => {
  loadingInstance.close();
};

/**
 * 显示全屏加载
 */
let needLoadingRequestCount = 0;
export const showFullScreenLoading = () => {
  if (needLoadingRequestCount === 0) {
    startLoading();
  }
  needLoadingRequestCount++;
};

/**
 *  隐藏全屏加载
 */
export const tryHideFullScreenLoading = () => {
  if (needLoadingRequestCount <= 0) return;
  needLoadingRequestCount--;
  if (needLoadingRequestCount === 0) {
    endLoading();
  }
};
